﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using md.imi.membranes.objects.alphabet;

namespace md.imi.membranes.gui.Editor
{
    public partial class AlphabetObjectEditor : Form
    {
        private AlphabetObject mAo;
        public AlphabetObjectEditor()
        {
            InitializeComponent();
        }

        public void LoadData(AlphabetObject ao)
        {
            mAo = ao;

            if (mAo.Data != null)
            {
                tbData.Text = mAo.Data;
                
            }

            gbInfo.Text = String.Format("Edit SO: <{0}>", mAo.Data);

            if (mAo.Indexes != null)
            {
                var sb = new StringBuilder();
                foreach (var idx in mAo.Indexes)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append(idx.ToString());
                }

                tbIndexes.Text = sb.ToString();
            }

            numMultiplicity.Value = mAo.Multiplicity;
            cbIsTerminal.Checked = mAo.IsTerminal;
        }

        public AlphabetObject GetData()
        {
            mAo.Data = tbData.Text;

            if (tbIndexes.Text.Length > 0)
            {
                mAo.Indexes = new List<int>();
                String[] indexes = tbIndexes.Text.Split(new char [] {','}, StringSplitOptions.RemoveEmptyEntries);

                foreach (var index in indexes)
                {
                    mAo.Indexes.Add( int.Parse(index));
                }
            }
            else
            {
                mAo.Indexes = null;
            }

            mAo.Multiplicity = (int)numMultiplicity.Value;
            mAo.IsTerminal = cbIsTerminal.Checked;

            String str = mAo.ToString();

            return mAo;
        }

        public bool ValidateData ()
        {
            errorProvider.Clear();

            if (tbData.Text.Trim().Length == 0)
            {
                errorProvider.SetError(tbData, "Cannot be empty or spaces");
                return false;
            }

            if (tbIndexes.Text.Length > 0)
            {
                String[] indexes = tbIndexes.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var index in indexes)
                {
                    int result = 0;
                    if (int.TryParse(index, out result) == false)
                    {
                        errorProvider.SetError(tbIndexes, "Wrong formated indexs (comma separated integer values are expected");
                        return false;
                    }
                }
            }

            if (numMultiplicity.Value < 1)
            {
                errorProvider.SetError(numMultiplicity, "Multiplicity should be bigger than 1");
                return false;
            }
            return true;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }
    }
}
